package cst.neu.yuejian_31.model;

import java.util.List;

import cst.neu.yuejian_31.bean.CollectClassBean;
import cst.neu.yuejian_31.iface.CollectClassIface;
import cst.neu.yuejian_31.iface.CollectClassListener;
import cst.neu.yuejian_31.service.CollectClassService;
import cst.neu.yuejian_31.Common.Constant;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/11/3.
 */

public class CollectClassModel implements CollectClassIface {
    private Retrofit retrofit;
    public CollectClassModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getClassCollect(final String userId, final CollectClassListener collectClassListener) {
        CollectClassService collectClassService=retrofit.create(CollectClassService.class);
        Call<List<CollectClassBean>>call=
                collectClassService.getClassCollect(userId);
        call.enqueue(new Callback<List<CollectClassBean>>() {
            @Override
            public void onResponse(Call<List<CollectClassBean>> call, Response<List<CollectClassBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    collectClassListener.onSuccess(response.body());
                }else {
                    collectClassListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<List<CollectClassBean>> call, Throwable t) {
                collectClassListener.onFail(t.toString());
            }
        });

    }
}
